<template>
  <div>
    <!-- 登陆页组件 -->
    <div class="main">
      <div class="login-form">
        <h2 class="login-title">会员管理系统-登陆</h2>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" type="password"></el-input>
          </el-form-item>
          <!-- 有些组件给他绑定事件不成功的时候，记得要加.native修饰符:作用就是把事件绑定到组件渲染的元素身上 -->
          <el-form-item prop="keep">
            <!-- <input type="checkbox" @click="keep()" name="" id=""> -->
            <el-checkbox v-model="ruleForm.checked" @click.native="keep($event)">保留7天免登陆</el-checkbox>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="float:right;" @click="submitForm('ruleForm')">登陆</el-button>
            <el-button style="float:right;margin-right:5px" @click="gotoReg">注册</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import usersApi from "@/userapi/usersApi"; //引入
import { setToken, setUser } from "@/utils/cookie"; //setToken :设置token  setUser:设置用户名
export default {
  data() {
    return {
      ruleForm: {
        name: "",
        password: "",
        checked: false
      },
      rules: {
        //正则
        name: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
          //   { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" }]
      }
    };
  },

  components: {},

  methods: {
    //跳转注册页面
    gotoReg() {
      this.$router.push("/reg");
    },
    //勾选七天免登录
    keep(ev) {
      //element复选框会触发两次
      if (ev.target.tagName == "INPUT") return;
      if (!this.ruleForm.checked) {
        //弹窗
        this.$message({
          message: "请不要在公共场合使用该功能",
          type: "error"
        });
      }
    },
    //登录
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          try {
            //加密
            let psw = this.$md5(this.ruleForm.password);
            let p = await usersApi.login(this.ruleForm.name, psw);
            if (p.data.flag) {
              this.$message({
                message: "登陆成功",
                type: "success"
              });
              //存储token和用户信息到本地
              let userinf = {
                name: this.ruleForm.name,
                psw: this.ruleForm.password
              };

              if (this.ruleForm.checked) {
                //如果选中勾选框就把token存入cookie 并保留七天
                setToken(p.data.token, 7);
                //吧用户信息存入cookie, 无法存入对象 用JSON.stringify转成字符串
                setUser(JSON.stringify(userinf), 7);
              } else {
                //否则就是会话级别 不设置时间 关闭浏览器就删除
                setToken(p.data.token);
                setUser(JSON.stringify(userinf));
              }
              //跳转到首页
              this.$router.push("/home");
            } else {
              this.$message({
                message: "登陆失败",
                type: "error"
              });
            }
          } catch (err) {
            console.log(err);
          }
        } else {
          this.$message({
            message: "服务器问题",
            type: "error"
          });
        }
      });
    }
  }
};
</script>

<style scoped>
.login-form {
  width: 350px;
  /* 上下间隙150px, 左右自动 */
  margin: 160px auto;
  /*透明背景色*/
  background-color: rgb(255, 255, 255, 0.8);
  padding: 30px;
  padding-right: 60px;
  /* 圆角 */
  border-radius: 20px;
}
.main {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url("../../assets/img/login.jpg");
  overflow: hidden;
}
/* 标题 */
.login-title {
  text-align: center;
  color: #303133;
  margin-bottom: 20px;
}
</style>